package com.googlecode.spruce.web.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.googlecode.spruce.dao.derby.ServiceDao;
import com.googlecode.spruce.domain.Service;

@Controller
@RequestMapping("/v1/service")
public class ServiceController
{

    @Autowired
    private ServiceDao serviceDao;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public @ResponseBody
    Service getById(@PathVariable Integer id)
    {
        return serviceDao.findByID(id);
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public @ResponseBody
    Integer create(@RequestBody final Service service)
    {
        Assert.notNull(service);
        Assert.hasText(service.getName(), "Host must have a name");
        Assert.hasText(service.getPluginName(), "Plugin name must be specified");
        Assert.notNull(service.getServiceGroupID(), "Service group ID must be specified");

        return serviceDao.save(service);
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public @ResponseBody
    List<Service> getAll()
    {
        return serviceDao.getAll();
    }

}
